const { executeCommand } = require('./utils')

const getCommitIdByTag = async (tag) => {
  return await executeCommand(`git rev-list -n 1 ${tag} | grep -oE "[0-9a-f]{40}"`)
}

/**
 * get latest tags with package name
 * @param {*} packageName
 * @param {*} count
 * @returns
 */
const getLatestTags = async (packageName, count = 2) => {
  return await executeCommand(
    `git tag --sort=-creatordate | grep '${packageName}' | grep -v 'alpha' | head -n ${count}`,
  )
}

/**
 * get latest commit id
 * @returns latest commit id
 */
const getLatestCommitId = async () => {
  return await executeCommand(`git rev-parse HEAD`)
}

module.exports = {
  getCommitIdByTag,
  getLatestTags,
  getLatestCommitId,
}
